package com.thssh.ui.lsn13.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/12
 * email: zhangyugehu@163.com
 */

public class CustomLayout extends ViewGroup {
    private boolean mOnTouchEventHandled;
    private boolean mOnInterceptTouchEventHandled;

    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    public void setOnTouchEventHandled(boolean handled){
        mOnTouchEventHandled = handled;
    }

    public void setOnInterceptTouchEventHandled(boolean handled) {
        this.mOnInterceptTouchEventHandled = handled;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        boolean handled = super.onInterceptTouchEvent(ev);
        boolean handled = mOnInterceptTouchEventHandled;

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                handled = super.onInterceptTouchEvent(ev);
                Log.d("CustomLayout", "onInterceptTouchEvent: ACTION_DOWN " + handled);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("CustomLayout", "onInterceptTouchEvent: ACTION_MOVE " + handled);
                break;
            case MotionEvent.ACTION_UP:
                Log.d("CustomLayout", "onInterceptTouchEvent: ACTION_UP " + handled);
                break;
            default:
                Log.d("CustomLayout", "onInterceptTouchEvent: " + ev.getAction() + " " + handled);
                break;
        }
        return handled;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for(int i = 0; i<childCount; i++){
            View child = getChildAt(i);
            child.layout(0, 0, 500, 500);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = mOnTouchEventHandled;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("CustomLayout", "onTouchEvent: ACTION_DOWN " + handled);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("CustomLayout", "onTouchEvent: ACTION_MOVE " + handled);
                break;
            case MotionEvent.ACTION_UP:
                Log.d("CustomLayout", "onTouchEvent: ACTION_UP " + handled);
                break;
            default:
                Log.d("CustomLayout", "onTouchEvent: " + event.getAction() + "  " + handled);
                break;
        }
        return handled;
    }

}
